package com.luxoft.scoremanager.testpack;

import org.apache.log4j.Logger;
import org.openqa.selenium.*;

import java.util.Random;

public class SeleniumHelper {
    WebDriver driver;
    private static final Logger LOG = Logger.getLogger(SeleniumHelper.class);

    public SeleniumHelper(WebDriver driver) {
        this.driver = driver;
    }


    public void xpathSendKeys(String xpath) {
        driver.findElement(By.xpath(xpath)).sendKeys();
    }

    public String getActualPackageState() throws InterruptedException {

        WebElement elem;
        int i = 0;
        do {
            Thread.sleep(100);

            i++;
            elem = driver.findElement(By.xpath("//span[contains(@id,'griddy_header')]"));
            if (i > 50) {
                throw new RuntimeException("Package data not loaded");

            }
        } while ("Package Manager".equals(elem.getText()));

        String stage = elem.getText().substring(15);

        return stage;
    }

    public String getActualActiveColumn() throws InterruptedException {
        if ("IN_COMMITTEE".equals(getActualPackageState())) {
            return "final";
        } else {
            return "exist";
        }
    }

    public WebElement getCellFromCurrentStage(String testColumn, int i) {
        i++;
        String xpath = "(//*[contains(@class, '" + testColumn + "')])[" + i + "]//div";
        LOG.info("getCellFromCurrentStage return xpath " + xpath);
        return driver.findElement(By.xpath(xpath));

    }

    public WebElement getElementFromActiveComboCell(int i) {
        String xpath = "//*[contains(@class, 'editorList')]//li[" + i + "]";

        return driver.findElement(By.xpath(xpath));
    }

    public int getNumberOfRows() {
        String xpath = "//*[contains(@class, 'exist')]";

        return driver.findElements(By.xpath(xpath)).size();

    }

        public int getRandomNumberOfComboBoxCell() {
        String xpath = "//*[contains(@class, 'exist')]";

        int elementSize = driver.findElements(By.xpath(xpath)).size();
        LOG.info("getRandomNumberOfComboBoxCell elementSize " + elementSize);

        Random random = new Random();
        LOG.info("getRandomNumberOfComboBoxCell random" + random);
        LOG.info("getRandomNumberOfComboBoxCell radnom " + elementSize);
        return elementSize - random.nextInt(elementSize);
    }


    public WebElement getRandomElementFromActiveComboCell() {
        String xpath = "//*[contains(@class, 'editorList')]//li";
        Random random = new Random();

        LOG.info("getRandomElementFromActiveComboCell radnom " + random);

        int elementSize = driver.findElements(By.xpath(xpath)).size();
        LOG.info("getRandomElementFromActiveComboCell elementSize " + elementSize);
        int randomInt = random.nextInt(elementSize) + 1;

        LOG.info("getRandomElementFromActiveComboCell randomInt " + randomInt);

        WebElement element = driver.findElement(By.xpath("//*[contains(@class, 'editorList')]//li[" + randomInt + "]"));

        LOG.info("getRandomElementFromActiveComboCell element " + element);
        LOG.info("getRandomElementFromActiveComboCell element " + element.getText());
        return element;

    }


    public boolean currentTextInComboBoxCellIsChanged(String testColumn, int i, String cellText) {
        String testCell = this.getCellFromCurrentStage(testColumn, i).getText();

        return !testCell.equals(cellText);
    }

    @Deprecated
    public static String getBrowserName(int browser) {

        String browserName = "";

        browserName.compareToIgnoreCase(browserName);


        switch (browser) {
            case 0:
                browserName = "Firefox";
                break;
            case 1:
                browserName = "Chrome";
                break;
            case 2:
                browserName = "Internet Explorer";
                break;
        }
        return browserName;
    }

}
